/**
 * @ author: 汪孟苹
 */
define(['jquery','bbcare',"bch/app/selfRecord/selfRecordComm.servers","bch/app/selfRecord/selfRecordComm.ui","bch/app/selfRecord/selfRecordComm.event","bch/app/selfRecord/selfRecord.servers","bch/app/selfRecord/selfRecord.ui"],function($,$bb,$selfRecordCommServers,$selfRecordCommUi,$selfRecordCommEvent,$servers,$ui){
	var selfRecord = {};

	/**
	 * 微信用户建档绑定模块 / 根据传入paramCode 查询问卷（新生儿早期记录1-7天问卷查询）
	 */
	var ajaxChildcareConDataCallBack = function(data,datas){
		var datas2 = $selfRecordCommServers.handleTable(datas,data.dayValueList);
		$selfRecordCommUi.table({
			"id":"selfRecordTable",
			"data":datas2
		});
	}

	/**
	 * 自我检查记录 / 查询患者任务问卷及答案请求成功后的回调函数
	 */
	var ajaxSelfRecordTemplCallBack = function(data,taskData,index){
		var datas = $selfRecordCommServers.handleTable(data);
		var id = index == 0 ? "selfRecordTable" : index == 1 ? "selfRecordTable2" : null;
		$selfRecordCommUi.table({
			"id":id,
			"data":datas
		});
		if( datas.flag ) $selfRecordCommServers.ajaxChildcareConData(taskData,ajaxChildcareConDataCallBack,data);
	}

	/**
	 * 选择xx自我记录
	 */
	var onTaskSelect = function(){
		$(this).addClass("active").siblings().removeClass("active");
		var index = $(this).index();
		var data = $(this).data("data");
		$selfRecordCommServers.ajaxSelfRecordTempl(data.rows[index],ajaxSelfRecordTemplCallBack,data.index);
	}

	/**
	 * 请求自我检查记录 / 查询患者所有任务成功后的回调函数
	 */
	var ajaxUserCpTasksCallBack = function(data,params){
		var id = params.index == 0 ? "tabShow" : params.index == 1 ? "tabShow2" : null;
		if( data.rows.length > 0 ){
			$selfRecordCommUi.tab({
				"id":id,
				"list":data.rows
			});
			// 初始化
			$selfRecordCommEvent.carouselInit(0,id);
			$("#" + id + " li").unbind("click");
			$("#" + id + " li").data("data",{"rows":data.rows,"index":params.index});
			$("#" + id + " li").click(onTaskSelect);
			$("#" + id + " li").eq(0).click();
		}else{
			if( params.index == 0 ) $("#selfRecordTable").html('<div class="selfRecord-none">该患者没有做育儿记录！</div>');
			if( params.index == 1 ) $("#selfRecordTable2").html('<div class="selfRecord-none">该患者没有做宝宝家庭安全自评！</div>');
		}
	}

	/**
	 * 牙齿自我记录查询成功后的回调函数
	 */
	var ajaxToothRecordsCallBack = function(data){
		$ui.tooth({
			"id":"tooth",
			"list":data.valueList,
		});
	}

	/**
	 * // 选择育儿记录、宝宝家庭安全自评、宝宝出牙记录
	 */
	var onClassifySelect = function(){
		var params = $(this).data("params");
		var index = $(this).index();
		$(this).addClass("active").siblings().removeClass("active");
		$(".selfRecord-con .con").eq(index).show().siblings().hide();
		if( index == 0 ){
			$.extend(params,{"busiTypeList":["A0101"],"subBusiTypeList":["B0104"],"index":index});
			$selfRecordCommServers.ajaxUserCpTasks(params,ajaxUserCpTasksCallBack);
		}else if( index == 1 ){
			$.extend(params,{"busiTypeList":["A0301"],"subBusiTypeList":["B0108"],"index":index});
			$selfRecordCommServers.ajaxUserCpTasks(params,ajaxUserCpTasksCallBack);
		}else if( index == 2 ){
			$.extend(params,{"index":index});
			$servers.ajaxToothRecords(params,ajaxToothRecordsCallBack);
		}
	}

	/**
	 * 患者基本信息
	 */
	var infoInit = function(info){
		$ui.info({
			"id":"selfRecordInfo",
			"data": info
		});
	}
	
	selfRecord.init = function(){
		var params = $bb.getPageParams();
		infoInit(params.info);
		// 选择育儿记录、宝宝家庭安全自评、宝宝出牙记录
		$(".selfRecord-classify > div").unbind("click");
		$(".selfRecord-classify > div").data("params",params);
		$(".selfRecord-classify > div").click(onClassifySelect);
		$(".selfRecord-classify > div").eq(0).click();
	};

	return selfRecord;
});